<?php
 
include_once("./configure/configure.php");						//--> global var
include_once("./admin/tools/smarty/libs/Smarty.class.php");		//--> out template
include_once("./admin/lib/utils/FileUtils.php");				//--> file utils

include_once("./lib/impl/CouponImpl.php");
include_once("./lib/impl/CouponAttributeImpl.php");

include_once("./lib/impl/CategoryImpl.php");
include_once("./lib/impl/MerchantImpl.php");

include_once("./lib/constant/GlobalConstant.php");

include_once("includeSmarty.php");

if(isset($_POST['id']))
	$id = $_POST['id'];
else
	$id = $_GET['id'];

if(empty($id)){
	$location = HOME_URL_HTTP;
	header("Location: $location");
				
	exit;
}

if(isset($_POST['page']))
	$page 	= $_POST['page'];
else
	$page	= 0;


$cacheId 	= $id . '_' . $page;

/*----- out html -----*/
$SUB_DIR	= FileUtils::getDirByString($id) . '/';

$smarty->compile_dir  	= COMPILE_DIR . '/tags/' . $SUB_DIR;
$smarty->cache_dir  	= CACHE_DIR . '/tags/' . $SUB_DIR;

if(!$smarty->isCached('tag.html', $cacheId, $cacheId)){
	$oCouponImpl 		= new CouponImpl(DB_TAG_COUPON);
	$oCAttributeImpl 	= new CouponAttributeImpl(DB_TAG_COUPON);
	
	$oCategoryImpl		= new CategoryImpl(DB_TAG_COUPON);
	$oMerchantImpl 		= new MerchantImpl(DB_TAG_COUPON);

	include_once("includeTag.php");
	include_once("includeCategories.php");
	
	$oTag 	= $oTagImpl->getByNameId($id);
	
	$orderStr	= " ORDER BY r_sortValue DESC";
		
	// get otherCoupon for the merchant
	if($id == 'Free_Shipping'){
		$where 		= " WHERE status = 1 and id in (SELECT couponId FROM coupon_attributes WHERE attributeId in(2, 35, 38, 37, 40))";
	
		$oCoupons 	= $oCouponImpl->getCustoms($where, $orderStr, $page, 15);
	}else{
		$oCoupons = $oCouponImpl->getCouponsByTag($oTag->getId(), $page, 15, $orderStr);
	}
	
	foreach ($oCoupons as $oCoupon) {
		$oCouponAttribute = $oCAttributeImpl->getLists($oCoupon->getId());
		$oCoupon->setAttributes($oCouponAttribute);
		
		$oMerchant 	= $oMerchantImpl->getById($oCoupon->getMerchantId());

		$oCoupon->setMerchant($oMerchant);

		$oCategory 	= $oCategoryImpl->getByCouponId($oCoupon->getId());
		
		if(!is_object($oCategory))
			$oCategory = new CategoryDomain();
		
		$oCoupon->setCategory($oCategory);
		
		if($oCoupon->getCreativeType() == 1){
			$oImage = $oCouponImpl->getCouponImage($oCoupon->getId());
			
			if(is_object($oImage)){
				$oCoupon->setCouponImage($oImage);
			}else{
				$oCoupon->setCreativeType(2);
			}
		}
		
		if($oCoupon->getCreativeType() == 2){
			$oLogo 		= $oMerchantImpl->getLogo($oCoupon->getMerchantId());
			
			if(!is_object($oLogo)){
				$oLogo = new ImageDomain();
				
				$oLogo->setId(0);
				$oLogo->setExtName('gif');
			}
			
			$oMerchant->setLogo($oLogo);
		}
		
	}
	
	$smarty->assign('oTag', $oTag);
	
	$smarty->assign('oCoupons', $oCoupons);
	$smarty->assign('oTags', $oTags);
	$smarty->assign('oCatTree', $oCatTree);
	
	$smarty->assign('pageList', $oCouponImpl->getPageList(5));
	$smarty->assign('nextPage', $oCouponImpl->getNextPage());
	$smarty->assign('prePage', $oCouponImpl->getPrePage());
	$smarty->assign('pageCount', $oCouponImpl->getPageCount());
	$smarty->assign('page', $oCouponImpl->getPage());
	
	$smarty->assign('isTag', 'yes');
	$smarty->assign('id', $id);
}

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL_HTTP);


$smarty->display('tag.html', $cacheId, $cacheId);
?>
